package cn.hit.tianqiao.web.servlet;

import cn.hit.tianqiao.domain.Order;
import cn.hit.tianqiao.domain.Reservation;
import cn.hit.tianqiao.service.ReservationService;
import cn.hit.tianqiao.service.impl.ReservationServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/reservations/*")
public class ReservationServlet extends HttpServlet {

    private ReservationService reservationService = new ReservationServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求和响应的字符编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        try {
            // 使用 ObjectMapper 从请求体中读取 JSON 数据
            ObjectMapper mapper = new ObjectMapper();
            Reservation reservationRequest = mapper.readValue(req.getInputStream(), Reservation.class);

            // 验证参数
            if (reservationRequest.getUserName() == null || reservationRequest.getUserPhone() == null || reservationRequest.getEventId() == 0) {
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().write("{\"error\":\"Missing required fields.\"}");
                return;
            }

            // 保存预约信息
            Reservation savedReservation = reservationService.saveReservation(reservationRequest);

            // 返回保存后的预约信息
            PrintWriter out = resp.getWriter();
            mapper.writeValue(out, savedReservation);
            out.flush();
        } catch (IOException e) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().write("{\"error\":\"Invalid request data.\"}");
        } catch (Exception e) {
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            resp.getWriter().write("{\"error\":\"An error occurred while processing your request.\"}");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置响应的字符编码
        resp.setContentType("application/json;charset=UTF-8");

        String userPhone = req.getParameter("userPhone");
        List<Reservation> reservations = new ArrayList<>(); // 实例化 List
        List<Order> orders = new ArrayList<>(); // 实例化 List

        if (userPhone == null || userPhone.isEmpty()) {
            // 获取所有预约信息
            reservations = reservationService.getAllReservations();
            // 返回预约信息列表
            ObjectMapper mapper = new ObjectMapper();
            PrintWriter out = resp.getWriter();
            mapper.writeValue(out, reservations);
            out.flush();
        } else {
            // 根据用户电话号码获取预约信息
            orders = reservationService.getReservationsByUserPhone(userPhone); // 传入用户电话号码参数
            // 返回预约信息列表
            ObjectMapper mapper = new ObjectMapper();
            PrintWriter out = resp.getWriter();
            mapper.writeValue(out, orders);
            out.flush();
        }
    }
}
